﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace NinjaToolbox.PhysicsSystem
{
    public sealed class PhysicsShapeContainerDrawer
    {
        PhysicsShapeContainer mPhysicsShapeContainer;
        SerializeField mSerializeField;

        IPhysicsShape mLastShape;
        ShapeSceneHandleDrawerBase mShapeSceneGUIDrawer;


        public void Initialization(PhysicsShapeContainer container)
        {
            mPhysicsShapeContainer = container;
        }

        public void DrawProperty(SerializedProperty serializeProperty, bool isMultipleObject)
        {
            var shapeTypeProp = serializeProperty.FindPropertyRelative("mType");
            var boxShapeOffsetProp = serializeProperty.FindPropertyRelative("mBoxShape.offset");
            var boxShapeSizeProp = serializeProperty.FindPropertyRelative("mBoxShape.size");
            var boxShapeForwardAxisModeProp = serializeProperty.FindPropertyRelative("mBoxShape.forwardAxisMode");

            var sphereShapeOffsetProp = serializeProperty.FindPropertyRelative("mSphereShape.offset");
            var sphereShapeRadiusProp = serializeProperty.FindPropertyRelative("mSphereShape.radius");

            var offsetProp = serializeProperty.FindPropertyRelative("mVirtualCapsuleShape.offset");
            var heightProp = serializeProperty.FindPropertyRelative("mVirtualCapsuleShape.height");
            var radiusProp = serializeProperty.FindPropertyRelative("mVirtualCapsuleShape.radius");
            var directionProp = serializeProperty.FindPropertyRelative("mVirtualCapsuleShape.direction");
            var virtualSegmentProp = serializeProperty.FindPropertyRelative("mVirtualCapsuleShape.virtualSegment");

            if (!isMultipleObject)
            {
                if (mShapeSceneGUIDrawer != null)
                    mShapeSceneGUIDrawer.DrawInspectorGUI();
            }

            EditorGUILayout.PropertyField(shapeTypeProp, new GUIContent("Type"));

            var physicsShapeType = (EPhysicsShapeType)shapeTypeProp.enumValueIndex;

            if (mPhysicsShapeContainer.GetPhysicsShape() != mLastShape)
            {
                if (mShapeSceneGUIDrawer != null)
                    mShapeSceneGUIDrawer.Dispose();

                switch (physicsShapeType)
                {
                    case EPhysicsShapeType.Box:
                        mShapeSceneGUIDrawer = new BoxShapeSceneHandleDrawer();
                        break;
                    case EPhysicsShapeType.Sphere:
                        mShapeSceneGUIDrawer = new SphereShapeSceneHandleDrawer();
                        break;
                    case EPhysicsShapeType.VirtualCapsule:
                        mShapeSceneGUIDrawer = new VirtualCapsuleShapeSceneHandleDrawer();
                        break;
                }

                mShapeSceneGUIDrawer.Initialization(serializeProperty.serializedObject, mPhysicsShapeContainer.GetPhysicsShape());

                mLastShape = mPhysicsShapeContainer.GetPhysicsShape();

                if (mShapeSceneGUIDrawer != null)
                    mShapeSceneGUIDrawer.DrawInspectorGUI();
            }

            switch (physicsShapeType)
            {
                case EPhysicsShapeType.Box:
                    EditorGUILayout.PropertyField(boxShapeOffsetProp);
                    EditorGUILayout.PropertyField(boxShapeSizeProp);
                    EditorGUILayout.PropertyField(boxShapeForwardAxisModeProp);
                    break;
                case EPhysicsShapeType.Sphere:
                    EditorGUILayout.PropertyField(sphereShapeOffsetProp);
                    EditorGUILayout.PropertyField(sphereShapeRadiusProp);
                    break;
                case EPhysicsShapeType.VirtualCapsule:
                    EditorGUILayout.PropertyField(offsetProp);
                    EditorGUILayout.PropertyField(heightProp);
                    EditorGUILayout.PropertyField(radiusProp);
                    EditorGUILayout.PropertyField(directionProp);
                    EditorGUILayout.PropertyField(virtualSegmentProp);
                    break;
                default:
                    break;
            }
        }

        public void Dispose()
        {
            if (mShapeSceneGUIDrawer != null)
                mShapeSceneGUIDrawer.Dispose();
        }
    }
}
